﻿/*----------------------------------------------------------------
    // Copyright (C) 2009 江苏华招网
    // 版权所有。 
    // 作者：庄金冬 
    //
    // 文件名：BLL_User.cs
    // 文件功能描述：用户操作业务逻辑类,实现接口IBLL_User,继承自类MarshalByRefObject
    // 创建标识：庄金冬2009-03-06
    //
    // 修改标识：
    // 修改描述：
    //
    // 修改标识：
    // 修改描述：
    //
    // 审核人：
    // 审核时间：
    // 审核说明：
 //----------------------------------------------------------------*/
using System;
using System.Collections.Generic;

using System.Text;
using HZ.Supervise.IBLL;
using HZ.Supervise.Model;
using HZ.Supervise.DAL;
using System.Data;

namespace HZ.Supervise.BLL
{
    [Serializable]
    /// <summary>
    /// 用户操作业务逻辑类,实现接口IBLL_User,继承自类MarshalByRefObject
    /// </summary>
    public class BLL_User : MarshalByRefObject, IBLL_User
    {
        #region IBLL_User 成员

        /// <summary>
        /// 获取所有用户
        /// </summary>
        /// <param name="currentPage">当前页</param>
        /// <param name="pageSize">每页显示记录数</param>
        /// <param name="totalRecord">（输出参数）总记录数目</param>
        /// <returns>用户集合</returns>
        public IList<User> GetAllUser(int currentPage, int pageSize, out int totalRecord)
        {
            

            HZ.Supervise.Model.Query.User QueryUser = new HZ.Supervise.Model.Query.User();
            return GetUserByWhere(QueryUser, currentPage, pageSize, out  totalRecord);
        }
         /// <summary>
        /// 新增删除数据
        /// </summary>
        /// <param name="userRuleAdd"></param>
        /// <param name="userRuleDelete"></param>
        /// <returns></returns>
        public int UpdateUserRule(IList<UserRule> userRuleAdd, IList<UserRule> userRuleDelete) 
        {
            return DAL_User.UpdateUserRule(userRuleAdd,userRuleDelete); 
        }
        /// <summary>
        /// 按角色获取所有主用户
        /// </summary>
        /// <param name="role">角色（Role仅RoleID须赋值）</param>
        /// <param name="currentPage">当前页</param>
        /// <param name="pageSize">每页显示记录数</param>
        /// <param name="totalRecord">（输出参数）总记录数目</param>
        /// <returns>主用户集合（不含权限角色等相关信息）</returns>
        public IList<User> GetAllUserByRole(Role role, int currentPage, int pageSize, out int totalRecord)
        {
           
            HZ.Supervise.Model.Query.User QueryUser = new HZ.Supervise.Model.Query.User();
            QueryUser.RoleID = role.RoleID;
            QueryUser.IsAdmin = true;
            return GetUserByWhere(QueryUser, currentPage, pageSize, out  totalRecord);
        }

        /// <summary>
        /// 根据编号获取用户（不含权限信息，角色信息，权限信息，机构信息）
        /// </summary>
        /// <param name="userid">用户编号</param>
        /// <returns></returns>
        public User GetUserByUserID(string userid)
        {
            
            return DAL_User.GetUserByUserID(userid);
        }

        /// <summary>
        /// 根据编号获取用户（包含用户信息，角色信息，权限信息，机构信息）
        /// </summary>
        /// <param name="userid">用户编号</param>
        /// <returns></returns>
        public User GetUserInfoByUserID(string userid)
        {
            
            return DAL_User.GetUserInfoByUserID(userid);
        }

        /// <summary>
        ///  获取某个主用户的子用户
        /// </summary>
        /// <param name="userid">主用户编号</param>
        /// <returns>用户集合</returns>
        public IList<User> GetChildUserByUserID(string userid)
        {
            return DAL_User_Role.GetChildUserByUserID(userid);
        }

        /// <summary>
        /// 更新用户信息[名称、是否启用](0,更新失败;1,更新成功;2,用户编号不存在;)
        /// </summary>
        /// <param name="user">用户[名称、是否启用]</param>
        /// <returns>(0,更新失败;1,更新成功;2,用户编号不存在;)</returns>
        public int UpdateUser(User user)
        {
            

            if (!string.IsNullOrEmpty(user.UserPassword))
            {
                user.UserPassword = HZ.Fun.GetMd5(user.UserPassword);

            }

            return DAL_User.UpdateUser(user, 0);
        }

        /// <summary>
        /// 获取用户的操作权限
        /// </summary>
        /// <param name="userid">用户编号</param>
        /// <returns>操作权限集合</returns>
        public IList<OperatingRule> GetUserOperatingRule(string userid)
        {
            return DAL_User.GetUserOperatingRule(userid);
        }
         /// <summary>
        /// 获取用户操作权限（2）特殊权限
        /// </summary>
        /// <param name="userid"></param>
        /// <returns></returns>
        public IList<OperatingRule> GetUserOperatingRuleSpecial(string userid) 
        {
            return DAL_User.GetUserOperatingRuleSpecial(userid);
        }
        /// <summary>
        /// 判断某个用户是否有该权限
        /// </summary>
        /// <param name="userID">用户编号</param>
        /// <param name="module">权限模块</param>
        /// <param name="OperatingRuleID">权限ID</param>
        /// <returns></returns>
        public bool CheckUserRule(string userID, HZ.Supervise.Model.Enum.Module module, HZ.Supervise.Model.Enum.OperateType operateType)
        {
            return DAL_User.CheckUserRule(userID, module, operateType);
        }

        /// <summary>
        /// 修改密码(0,更新失败;1,更新成功;2,原密码错误)
        /// </summary>
        /// <param name="userid">用户编号</param>
        /// <param name="oldpassword">原始密码</param>
        /// <param name="newpassword">新密码</param>
        /// <returns>(0,更新失败;1,更新成功;2,原密码错误)</returns>
        public int UpdatePassword(string userid, string oldpassword, string newpassword)
        {
            oldpassword = HZ.Fun.GetMd5(oldpassword);
            newpassword = HZ.Fun.GetMd5(newpassword);
            
            int result = DAL_User.UpdatePassword(userid, oldpassword, newpassword, 1);
            //if (result == 1) { DAL_Helper.OperateTableSQL(new KeyOperationLogJG() { OperationLogSerialID = Guid.NewGuid().ToString(), OperateMessage = "修改密码成功", OperateTime = DateTime.Now, Operator = userid }, TableOperator.Insert); }
            return result;
        }

        /// <summary>
        /// 密码初始化
        /// </summary>
        /// <param name="userid">用户编号</param>
        /// <param name="newpassword">初始化密码</param>
        /// <returns></returns>
        public bool InitPassword(string userid, string newpassword)
        {
            
            newpassword = HZ.Fun.GetMd5(newpassword);

            return DAL_User.UpdatePassword(userid, "", newpassword, 0) == 1;
        }

        /// <summary>
        /// 密码初始化
        /// </summary>
        /// <param name="userids">用户编号集合</param>
        /// <param name="newpassword">初始化密码</param>
        /// <returns></returns>
        public bool InitPassword(List<string> userids, string newpassword)
        {
            newpassword = HZ.Fun.GetMd5(newpassword);
            
            //处理用户编号
            StringBuilder sbUserIDs = new StringBuilder("");
            string strUserIDs = "";
            if (userids != null)
            {
                if (userids.Count > 0)
                {
                    foreach (string UserID in userids)
                    {
                        sbUserIDs.Append(UserID + ",");
                    }

                    strUserIDs = sbUserIDs.ToString().Trim().Substring(0, sbUserIDs.Length - 1);

                }
            }


            return DAL_User.ResetUserPassword(strUserIDs, newpassword) == 1;
        }


        /// <summary>
        /// 用户锁定
        /// </summary>
        /// <param name="userid">用户编号</param>
        /// <returns></returns>
        public bool LockUser(string userid)
        {
            
            return DAL_User.LockUser(userid);
        }


        /// <summary>
        /// 用户解锁
        /// </summary>
        /// <param name="userid">用户编号</param>
        /// <returns></returns>
        public bool UnblockUser(string userid)
        {
            
            return DAL_User.UnblockUser(userid);
        }


        /// <summary>
        /// 用户锁定（批量）
        /// </summary>
        /// <param name="userids">用户编号集合</param>
        /// <returns></returns>
        public bool LockUser(List<string> userids)
        {
            
            //处理用户编号
            StringBuilder sbUserIDs = new StringBuilder("");
            string strUserIDs = "";
            if (userids != null)
            {
                if (userids.Count > 0)
                {
                    foreach (string UserID in userids)
                    {
                        sbUserIDs.Append(UserID + ",");
                    }

                    strUserIDs = sbUserIDs.ToString().Trim().Substring(0, sbUserIDs.Length - 1);

                }
            }


            return DAL_User.LockOrUnblockUser(strUserIDs, 0) == 1;
        }

        /// <summary>
        /// 用户解锁（批量）
        /// </summary>
        /// <param name="userids">用户编号集合</param>
        /// <returns></returns>
        public bool UnblockUser(List<string> userids)
        {
            

            //处理用户编号
            StringBuilder sbUserIDs = new StringBuilder("");
            string strUserIDs = "";
            if (userids != null)
            {
                if (userids.Count > 0)
                {
                    foreach (string UserID in userids)
                    {
                        sbUserIDs.Append(UserID + ",");
                    }

                    strUserIDs = sbUserIDs.ToString().Trim().Substring(0, sbUserIDs.Length - 1);

                }
            }


            return DAL_User.LockOrUnblockUser(strUserIDs, 1) == 1;
        }

        /// <summary>
        /// 删除用户(0,删除失败;1,删除成功;2,该用户不存在;)
        /// </summary>
        /// <param name="userid">用户编号</param>
        /// <returns>(0,删除失败;1,删除成功;2,该用户不存在;)</returns>
        public int DeleteUser(string userid)
        {
            
            return DAL_User.DeleteUser(userid);
        }

        /// <summary>
        /// 添加用户同时设置权限(0,添加失败;1,添加成功;2,用户编号已存在;)
        /// </summary>
        /// <param name="user">用户对象（每个用户的UserRole仅RoleID须赋值，Org仅OrgID须赋值）</param>
        /// <param name="ruleIList">操作权限集合（每个权限仅RuleID须赋值）</param>
        /// <returns>(0,添加失败;1,添加成功;2,用户编号已存在;)</returns>
        public int AddUser_OperatingRule(User user, IList<OperatingRule> ruleIList)
        {
            
            if (!string.IsNullOrEmpty(user.UserPassword))
            {
                user.UserPassword = HZ.Fun.GetMd5(user.UserPassword);

            }
            return DAL_User.AddUser_OperatingRule(user, ruleIList);
        }
        /// <summary>
        /// 更新用户信息[是否疾控](0,更新失败;1,更新成功;)
        /// </summary>
        /// <param name="user">用户</param>
        /// <returns>(0,更新失败;1,更新成功;)</returns>
        public int UpdateUser_IsJK(User user)
        {
            return DAL_User.UpdateUser_IsJK(user);
        }
        /// <summary>
        /// 设置用户的操作权限
        /// </summary>
        /// <param name="userid">用户编号</param>
        /// <param name="ruleIList">操作权限集合</param>
        /// <returns></returns>
        public bool SetUserOperatingRule(string userid, IList<OperatingRule> ruleIList)
        {
            
            return DAL_User.SetUserOperatingRule(userid, ruleIList);
        }
         /// <summary>
        /// 查询出所用有户权限信息
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        public IList<UserRule> GetUserRuleDetailBywhere(string where) 
        {
            return DAL_User.GetUserRuleDetailBywhere(where);
        }
         /// <summary>
        /// 条件查询
        /// </summary>
        /// <param name="user">查询用实体</param>
        /// <param name="currentPage">当前页</param>
        /// <param name="pageSize">每页显示记录数</param>
        /// <param name="totalRecord">（输出参数）总记录数目</param>
        /// <returns></returns>
        public  IList<User> GetUserDetailByWhere(string where, int currentPage, int pageSize, out int totalRecord) 
        {
            return DAL_User.GetUserDetailByWhere(where,currentPage,pageSize,out totalRecord);
        }

        /// <summary>
        /// 条件查询
        /// </summary>
        /// <param name="user">查询用实体</param>
        /// <param name="currentPage">当前页</param>
        /// <param name="pageSize">每页显示记录数</param>
        /// <param name="totalRecord">（输出参数）总记录数目</param>
        /// <returns></returns>
        public IList<User> GetUserByWhere(HZ.Supervise.Model.Query.User user, int currentPage, int pageSize, out int totalRecord)
        {
            
            return DAL_User.GetUserByWhere(user, currentPage, pageSize, out totalRecord);
        }


        /// <summary>
        /// 更新用户信息[名称、是否启用]同时设置权限(0,更新失败;1,更新成功;2,用户编号不存在;)
        /// </summary>
        /// <param name="user">用户[名称、是否启用、权限集合]</param>
        /// <returns>(0,更新失败;1,更新成功;2,用户编号不存在;)</returns>
        public int UpdateUserRule(User user)
        {
            
            return DAL_User.UpdateUserRule(user);
        }

        /// <summary>
        /// 授权(0,失败;1,成功;)
        /// </summary>
        /// <param name="Orgs">机构字典(key:OrgID,value:RoleID)</param>
        /// <returns>(0,失败;1,成功;)</returns>
        public int AuthorizeOrg(IDictionary<string, string> Orgs)
        {
            
            return DAL_User.AuthorizeOrg(Orgs, 1);
        }

        /// <summary>
        /// 撤权(0,失败;1,成功;)
        /// </summary>
        /// <param name="Orgs">机构字典(key:OrgID,value:RoleID)</param>
        /// <returns>(0,失败;1,成功;)</returns>
        public int RevokeOrg(IDictionary<string, string> Orgs)
        {
            
            return DAL_User.AuthorizeOrg(Orgs, 0);
        }

        /// <summary>
        /// 条件机构用户查询（含机构角色相关信息）
        /// </summary>
        /// <param name="user">查询用实体</param>
        /// <param name="currentPage">当前页</param>
        /// <param name="pageSize">每页显示记录数</param>
        /// <param name="totalRecord">（输出参数）总记录数目</param>
        /// <returns></returns>
        public IList<HZ.Supervise.Model.View.OrgUsers> GetOrgUsersByWhere(HZ.Supervise.Model.Query.OrgUsers user, int currentPage, int pageSize, out int totalRecord)
        { 
            return DAL_User.GetOrgUsersByWhere(user, currentPage, pageSize, out totalRecord);
        }

   
        public User GetAdminUserByID(string userid)
        { 
            string adminuserid = DAL.DAL_User.GetAdminUserID(userid);
            return GetUserInfoByUserID(adminuserid);
        }
         
        #endregion
         
       /// <summary>
       /// Autor：曹鹏
       /// TIME：2011年4月7日20:00:38
        /// NOTE：条件机构用户查询（含机构角色相关信息）
        /// </summary>
        /// <param name="user">查询用实体</param>
        /// <param name="currentPage">当前页</param>
        /// <param name="pageSize">每页显示记录数</param>
        /// <param name="totalRecord">（输出参数）总记录数目</param>
        /// <returns></returns>
        public IList<HZ.Supervise.Model.View.OrgUsers> GetOrgUsersByWhere2(HZ.Supervise.Model.Query.OrgUsers user, int currentPage, int pageSize, out int totalRecord)
        {
            return DAL_User.GetOrgUsersByWhere2(user, currentPage, pageSize, out totalRecord);
        }
        /// <summary>
        /// 添加日志(0,添加失败;1,添加成功;)
        /// </summary>
        /// <param name="user">日志对象（每个日志的Operator，OrgID必须赋值）</param>
        /// <returns>(0,添加失败;1,添加成功;)</returns>
        public int AddKeyOperationLog(KeyOperationLogJG keyOperationLog)
        {
            return DAL_Helper.OperateTableSQL(keyOperationLog, TableOperator.Insert);
        }
        /// <summary>
        /// 平台基础信息---医院基础信息
        /// </summary> 
        public DataTable GetHospital(string str, int currentPage, int pageSize, out int totalRecord)
        {
            return DAL_User.GetHospital(str, currentPage, pageSize, out totalRecord);
        }
    }
}
